package com.gitlab.lucky.common.service;

import com.gitlab.lucky.common.dao.mapper.PlatformDataMapper;
import com.gitlab.lucky.common.dao.model.PlatformData;
import com.gitlab.lucky.common.enums.Constant;
import com.gitlab.lucky.common.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @program: lucky
 * @author: Mr.Han
 * @create: 2018-12-10 21:13
 **/
@Service
public class PlatformDataService extends ServiceImpl<PlatformDataMapper,PlatformData>{
    @Autowired
    private RedisService redisService;
    @Autowired
    private PlatformDataMapper mapper;
    //取平台信息
    public PlatformData getPlatform(Integer platform){
        PlatformData platformData = (PlatformData)redisService.get(Constant.PLATFORM_KEY+platform);
        if(platformData==null) {
            Example example = new Example(PlatformData.class);
            example.createCriteria().andEqualTo("platform", platform);
            List<PlatformData> platformDataList = this.selectByExample(example);
            if (platformDataList != null && platformDataList.size() != 0) {
                redisService.set(Constant.PLATFORM_KEY+platform,platformDataList.get(0));
                return platformDataList.get(0);
            }
        }
        return platformData;
    }
    public PlatformData selectByAppId(String appId){
        Example example = new Example(PlatformData.class);
        example.createCriteria().andEqualTo("appId",appId);
        List<PlatformData> platformData = this.selectByExample(example);
        if(platformData==null||platformData.size()==0){
            return null;
        }
        return platformData.get(0);
    }

    public PlatformData getPlatformDataByOpenId(String openId){
        Example example = new Example(PlatformData.class);
        example.createCriteria().andEqualTo("openId", openId);
        List<PlatformData> platformDataList = this.selectByExample(example);
        if (platformDataList != null && platformDataList.size() != 0) {
            return platformDataList.get(0);
        }
        return null;
    }
}
